/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 *
 * Created on 2009-11-10
 *******************************************************************************/

package org.ayound.skin.editor.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;

import org.ayound.skin.editor.core.SkinBuilder;
import org.ayound.skin.editor.core.convert.image.ImageConverter;
import org.ayound.skin.editor.core.convert.model.SkinModel;

public class MainFrame extends JFrame {

	private ImageSize imagePanel;

	private SkinModel model = new SkinModel();

	private JSlider hueSlider;

	private JSlider saturationSlider;

	private JSlider brightnessSlider;

	private JSlider redSlider;

	private JSlider blueSlider;

	private JSlider greenSlider;

	private String sourceFile = "e:\\skin\\test.gif";

	private boolean update;

	private Thread thread = null;

	private JDialog dialog;

	private void update() {
		setUpdate(true);
	}

	public boolean isUpdate() {
		return update;
	}

	synchronized public void setUpdate(boolean update) {
		this.update = update;
	}

	public void clear() {
		model = new SkinModel();
		hueSlider.setValue(0);
		saturationSlider.setValue(100);
		brightnessSlider.setValue(100);
		redSlider.setValue(0);
		greenSlider.setValue(0);
		blueSlider.setValue(0);
	}

	public MainFrame() {
		setResizable(false);
		Container content = getContentPane();
		content.setBackground(Color.white);
		JPanel centerContainer = new JPanel();
		imagePanel = new ImageSize();
		centerContainer.add(imagePanel);
		add(centerContainer);
		initMenu();
		initDialog();
		update();
	}

	private void initDialog() {
		dialog = new JDialog(this, "Skin Dialog", true);
		JPanel centerContainer = new JPanel();
		centerContainer.setLayout(null);
		JLabel hueLabel = new JLabel("hue");
		centerContainer.add(hueLabel);
		hueLabel.setBounds(20, 20, 100, 20);
		hueSlider = new JSlider(0, 360, 0);
		hueSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				model.setHue(hueSlider.getValue());
				update();
			}
		});
		hueSlider.setBounds(140, 20, 400, 20);
		centerContainer.add(hueSlider);
		JLabel saturationLabel = new JLabel("saturation");
		saturationLabel.setBounds(20, 50, 100, 20);
		centerContainer.add(saturationLabel);
		saturationSlider = new JSlider(0, 100, 100);
		saturationSlider.setBounds(140, 50, 400, 20);
		saturationSlider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {
				model.setSaturation(saturationSlider.getValue());
				update();
			}
		});
		centerContainer.add(saturationSlider);

		JLabel brightnessLabel = new JLabel("brightness");
		centerContainer.add(brightnessLabel);
		brightnessLabel.setBounds(20, 80, 100, 20);
		brightnessSlider = new JSlider(0, 100, 100);
		brightnessSlider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {
				model.setBrightness(brightnessSlider.getValue());
				update();
			}
		});
		centerContainer.add(brightnessSlider);
		brightnessSlider.setBounds(140, 80, 400, 20);
		JLabel redLabel = new JLabel("red");
		redLabel.setBounds(20, 110, 100, 20);
		centerContainer.add(redLabel);
		redSlider = new JSlider(0, 255, 0);
		redSlider.setBounds(140, 110, 400, 20);
		redSlider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {
				model.setRed(redSlider.getValue());
				update();
			}
		});
		centerContainer.add(redSlider);
		JLabel greenLabel = new JLabel("green");
		centerContainer.add(greenLabel);
		greenLabel.setBounds(20, 140, 100, 20);
		greenSlider = new JSlider(0, 255, 0);
		greenSlider.setBounds(140, 140, 400, 20);
		greenSlider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {
				model.setGreen(greenSlider.getValue());
				update();
			}
		});
		centerContainer.add(greenSlider);
		JLabel blueLabel = new JLabel("blue");
		blueLabel.setBounds(20, 170, 100, 20);
		centerContainer.add(blueLabel);

		blueSlider = new JSlider(0, 255, 0);
		blueSlider.setBounds(140, 170, 400, 20);
		blueSlider.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {
				model.setBlue(blueSlider.getValue());
				update();
			}
		});
		centerContainer.add(blueSlider);
		dialog.add(centerContainer, BorderLayout.CENTER);
		dialog.setBounds(200, 100, 600, 320);
		dialog.setResizable(false);
		JPanel panel = new JPanel();
		dialog.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER));
		JButton okBtn = new JButton("Ok");
		okBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(false);

			}
		});
		panel.add(okBtn);
		JButton cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				clear();
				dialog.setVisible(false);

			}
		});
		panel.add(cancelBtn);

	}

	public void updateImage() {
		try {
			ImageConverter converter = new ImageConverter(model);
			File srcFile = new File(sourceFile);
			File tempFile = File.createTempFile("test", srcFile.getName());
			converter.convert(sourceFile, tempFile.getAbsolutePath());
			BufferedImage image = ImageIO.read(tempFile);
			imagePanel.setImage(image);
			imagePanel.setSize(image.getWidth(), image.getHeight());
			Dimension us = imagePanel.getSize();
			Dimension them = imagePanel.getParent().getSize();
			int newX = (them.width - us.width) / 2;
			int newY = (them.height - us.height) / 2;
			imagePanel.setLocation(newX, newY);
		} catch (Exception e1) {

		}
	}

	private void initMenu() {
		MenuBar menuBar = new MenuBar();
		setMenuBar(menuBar);
		Menu fileMenu = new Menu();
		menuBar.add(fileMenu);
		fileMenu.setLabel("File");
		MenuItem openItem = new MenuItem("Open");
		openItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(false);
				chooser.setFileFilter(new FileFilter() {

					@Override
					public boolean accept(File file) {
						if (file.isDirectory()) {
							return true;
						}
						String name = file.getName().toLowerCase();
						if (name.endsWith(".gif") || name.endsWith(".jpg")
								|| name.endsWith(".bmp")
								|| name.endsWith(".png")
								|| name.endsWith(".jpeg")) {
							return true;
						}
						return false;
					}

					@Override
					public String getDescription() {
						return "(*.bmp,*.gif,*.jpg,*.jpeg,*.png)";
					}
				});
				int option = chooser.showOpenDialog(MainFrame.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					File selectFile = chooser.getSelectedFile();
					setSourceFile(selectFile.getAbsolutePath());
					clear();
					updateImage();
				}

			}
		});
		fileMenu.add(openItem);

		MenuItem exitItem = new MenuItem("Exit");
		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});
		fileMenu.add(exitItem);

		Menu skinMenu = new Menu("Skin");
		menuBar.add(skinMenu);

		MenuItem colorItem = new MenuItem("Color");
		skinMenu.add(colorItem);
		skinMenu.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(true);

			}
		});

		MenuItem saveItem = new MenuItem("Export");
		skinMenu.add(saveItem);
		saveItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(false);
				chooser.setFileFilter(new FileFilter() {

					@Override
					public boolean accept(File f) {
						return f.isDirectory();
					}

					@Override
					public String getDescription() {
						return null;
					}

				});
				int option = chooser.showOpenDialog(MainFrame.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					File selectFile = chooser.getSelectedFile();
					if (selectFile.isDirectory()) {
						SkinBuilder.buildSkin(selectFile, model);
					}
				}

			}
		});

		thread = new Thread() {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {

					}
					if (isUpdate()) {
						updateImage();
						setUpdate(false);
					}
				}
			}

		};
		thread.start();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame frame = new MainFrame();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.pack();
		Dimension them = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setSize(them.width, them.height - 50);
		frame.setVisible(true);
	}

	public String getSourceFile() {
		return sourceFile;
	}

	public void setSourceFile(String sourceFile) {
		this.sourceFile = sourceFile;
	}
}
